package net.tbao.order.listener;

import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import net.tbao.common.to.mq.SecKillOrderTo;
import net.tbao.order.entity.OrderEntity;
import net.tbao.order.service.OrderService;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Slf4j
@Component
@RabbitListener(queues = "order.seckill.order.queue")
public class OrderScekillListener {
    @Autowired
    OrderService orderService;


    /**
     * 关闭订单
     * @param entity
     * @param channel
     * @param message
     * @throws IOException
     */
    @RabbitHandler
    public void listenner(SecKillOrderTo secKillOrderTo, Channel channel, Message message) throws IOException, IOException {

        try {
            log.info("准备创建秒杀单的详细信息");

            orderService.createSeckillOrder(secKillOrderTo);
            //TODO 手动调用支付宝收单,预防网络异常和服务器宕机

            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (Exception e) {
            channel.basicReject(message.getMessageProperties().getDeliveryTag(), true);
        }

    }
}
